struct tm -> std::tm, memset of tm goes to {} (#1079)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sun, 23 Apr 2023 20:17:25 +0000 (14:17 -0600)
committerGitHub <noreply@github.com>
Sun, 23 Apr 2023 20:17:25 +0000 (14:17 -0600)
* struct tm -> std::tm, memset of tm goes to {}.

* init std::tm related variables.

tidy found found the following warnigns related to our usage of
std::tm:
cppcoreguidelines-pro-type-member-init
and std::tm*:
cppcoreguidelines-init-variables

* make GPS_D600_Get var tm automatic storage duration

and intialize it.

13 files changed:
brauniger_iq.cc
defs.h
garmin_txt.cc
gdb.cc
globalsat_sport.cc
jeeps/gpsapp.cc
navilink.cc
sbn.cc
trackfilter.cc
unicsv.cc
util.cc
wbt-200.cc
xcsv.cc

index 371096779c6857ae4108e3c3eb6daef183c35f6e..672326356446e2da3a987e9d99ef6322c7ae1eba 100644 (file)
@@ -75,7 +75,7 @@ static void rd_deinit()
 static int process_data(const unsigned char* data)
 {
   static int remaining = 100;
-  static struct tm tm;
+  static std::tm tm;
   static time_t start, creation;
   static route_head* track;
   static unsigned char interval;
diff --git a/defs.h b/defs.h
index 6fe4c28b1b0c1bf39e5483d9ed83e82aadee4d2f..b01100b7e1c4479265b4088a11e993608c84bf38 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -1024,8 +1024,8 @@ int str_match(const char* str, const char* match);
 int xvasprintf(char** strp, const char* fmt, va_list ap);
 char* strupper(char* src);
 char* strlower(char* src);
-time_t mklocaltime(struct tm* t);
-time_t mkgmtime(struct tm* t);
+time_t mklocaltime(std::tm* time);
+time_t mkgmtime(std::tm* time);
 bool gpsbabel_testmode();
 gpsbabel::DateTime current_time();
 QDateTime dotnet_time_to_qdatetime(long long dotnet);
index 24d7e4dd3184a1c8f2f8ae496c20873aead460d7..415a48a818807f94502fd6bc290044e62d333ff2 100644 (file)
@@ -365,7 +365,7 @@ print_position(const Waypoint* wpt)
 static void
 print_date_and_time(const time_t time, const int time_only)
 {
-  struct tm tm;
+  std::tm tm{};
   char tbuf[32];
 
   if (time < 0) {
diff --git a/gdb.cc b/gdb.cc
index dd4c2bc33c43f3637c31a0bf680731af7f91f2bf..d8b8648773b4ccd186a26616b49254a655bd863a 100644 (file)
--- a/gdb.cc
+++ b/gdb.cc
@@ -1108,7 +1108,6 @@ GdbFormat::write_header() const
   char buff[128], tbuff[32];
   char* c;
   int len, n = 0;
-  struct tm tm;
 
   FWRITE("MsRc", 4); // Signature
   FWRITE_i16(0x66);  // Primary File Format
@@ -1142,8 +1141,7 @@ GdbFormat::write_header() const
 
   */
 
-  memset(&tm, 0, sizeof(tm));
-
+  std::tm tm{};
   n = sscanf(gdb_release_date+7, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
   if (n != 6) {
     // The $Date string in gdb_release_date[] above is bad.
@@ -1155,7 +1153,7 @@ GdbFormat::write_header() const
 
   n = strftime(tbuff, sizeof(tbuff), "%b %d %Y*%H:%M:%S", &tm);
   if (n == 0) {
-    // The build of the struct tm was bad.
+    // The build of the std::tm was bad.
     fatal(MYNAME ": internal date generation error for %s\n", gdb_release_date + 7);
   }
 
index 8cef720e8adc25de7b51bdb8ea0be26206f2d1ea..813586728e9cc4108b3bcdcfe128948c9ce0e1a1 100644 (file)
@@ -520,8 +520,8 @@ GlobalsatSportFormat::track_read()
           }
 
           /*
-           * GPS year: 2000+; struct tm year: 1900+
-           * GPS month: 1-12, struct tm month: 0-11
+           * GPS year: 2000+; std::tm year: 1900+
+           * GPS month: 1-12, std::tm month: 0-11
            */
 
           QDate gpsDate = QDate(header.dateStart.Year+2000, header.dateStart.Month, header.dateStart.Day);
index 1d28ed0efd23d6d83810cff1f52e2609bf40ac04..0eb1159393b181cbaa8c73d02795486b3cf97472 100644 (file)
@@ -5872,7 +5872,7 @@ int32 GPS_A600_Send(const char* port, time_t Time)
 time_t GPS_D600_Get(GPS_PPacket& packet)
 {
   UC* p;
-  static struct tm ts;
+  std::tm ts{};
 
   p = packet.data;
 
@@ -5901,12 +5901,10 @@ time_t GPS_D600_Get(GPS_PPacket& packet)
 void GPS_D600_Send(GPS_PPacket& packet, time_t Time)
 {
   UC data[10];
-  UC* p;
-  struct tm* ts;
 
-  p = data;
+  UC* p = data;
 
-  ts = localtime(&Time);
+  std::tm* ts = localtime(&Time);
   *p++ = ts->tm_mon+1;
   *p++ = ts->tm_mday;
 
index d08c50564e2749df2311369fd30d2ad819e26baa..42da11b3a9d7cf83c16fdc9141f1f0f4ff1a080e 100644 (file)
@@ -366,9 +366,7 @@ decode_datetime(const unsigned char* buffer)
 static void
 encode_datetime(time_t datetime, unsigned char* buffer)
 {
-  struct tm* tm;
-
-  if ((tm = gmtime(&datetime)) != nullptr) {
+  if (std::tm* tm = gmtime(&datetime); tm != nullptr) {
     buffer[0] = tm->tm_year - 100;
     buffer[1] = tm->tm_mon + 1;
     buffer[2] = tm->tm_mday;
@@ -799,9 +797,7 @@ decode_sbp_datetime_packed(const unsigned char* buffer)
    * SSSSSSMM MMMMHHHH Hdddddmm mmmmmmmm
    */
 
-  struct tm tm;
-
-  memset(&tm, 0, sizeof(tm));
+  std::tm tm{};
 
   tm.tm_sec = buffer[0] & 0x3F;
   tm.tm_min = ((buffer[0] & 0xC0) >> 6) | ((buffer[1] & 0x0F) << 2);
diff --git a/sbn.cc b/sbn.cc
index 7ee6edcc083731d8d9f93c521523bc3987d746a9..f286690264d0121dc57bf1fc28eaa3ed451046b3 100644 (file)
--- a/sbn.cc
+++ b/sbn.cc
@@ -220,7 +220,7 @@ decode_sbn_mode(const unsigned char* mode)
 static void
 decode_sbn_datetime(const unsigned char* buffer, Waypoint* waypt)
 {
-  struct tm tm;
+  std::tm tm{};
   int ms = be_readu16(buffer + 6);
 
   tm.tm_sec = ms / 1000;
index b966a93554888b07d34f291eba1c9142ce1506d7..cd870212c2a12c2dbf5cd44781700c4fe00b1f37 100644 (file)
@@ -252,7 +252,7 @@ void TrackFilter::trackfilter_split_init_rte_name(route_head* track, const gpsba
       // Uggh.  strftime format exposed to user.
 
       time_t time = dt.toTime_t();
-      struct tm tm = *gmtime(&time);
+      std::tm tm = *gmtime(&time);
       char buff[128];
       strftime(buff, sizeof(buff), opt_title, &tm);
       track->rte_name = buff;
@@ -279,7 +279,7 @@ void TrackFilter::trackfilter_pack_init_rte_name(route_head* track, const gpsbab
       dt = wpt->GetCreationTime();
     }
     time_t t = dt.toTime_t();
-    struct tm tm = *gmtime(&t);
+    std::tm tm = *gmtime(&t);
     char buff[128];
     strftime(buff, sizeof(buff), opt_title, &tm);
     track->rte_name = buff;
index 743ebbc31ca9c78f055271087f8c0d7ee6889bc1..03c3147c47a8a4032919492a61f308963f89bfdf 100644 (file)
--- a/unicsv.cc
+++ b/unicsv.cc
@@ -23,7 +23,7 @@
 
 #include <cmath>                   // for fabs, lround
 #include <cstdio>                  // for NULL, sscanf
-#include <cstring>                 // for memset, strchr, strncpy
+#include <cstring>                 // for strchr, strncpy
 #include <ctime>                   // for gmtime
 
 #include <QByteArray>              // for QByteArray
@@ -230,10 +230,9 @@ UnicsvFormat::unicsv_parse_date(const char* str, int* consumed)
 {
   int p1, p2, p3;
   char sep[2];
-  struct tm tm;
+  std::tm tm{};
   int lconsumed = 0;
 
-  memset(&tm, 0, sizeof(tm));
   int ct = sscanf(str, "%d%1[-.//]%d%1[-.//]%d%n", &p1, sep, &p2, sep, &p3, &lconsumed);
   if (consumed && lconsumed) {
     *consumed = lconsumed;
@@ -348,7 +347,7 @@ UnicsvFormat::unicsv_adjust_time(const time_t time, const time_t* date) const
   if (opt_utc) {
     res += xstrtoi(opt_utc, nullptr, 10) * SECONDS_PER_HOUR;
   } else {
-    struct tm tm = *gmtime(&res);
+    std::tm tm = *gmtime(&res);
     res = mklocaltime(&tm);
   }
   return QDateTime::fromSecsSinceEpoch(res, Qt::UTC);
@@ -502,7 +501,7 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
   char is_localtime = 0;
   garmin_fs_t* gmsd;
   double d;
-  struct tm ymd;
+  std::tm ymd{};
   int src_datum = unicsv_datum_idx;
   int ns = 1;
   int ew = 1;
@@ -510,7 +509,6 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
   auto* wpt = new Waypoint;
   wpt->latitude = kUnicsvUnknown;
   wpt->longitude = kUnicsvUnknown;
-  memset(&ymd, 0, sizeof(ymd));
 
   int column = -1;
   const QStringList values = csv_linesplit(ibuf, unicsv_fieldsep, "\"", 0, CsvQuoteMethod::rfc4180);
@@ -967,8 +965,7 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
       time_t t = date + time;
 
       if (is_localtime) {
-        struct tm tm;
-        tm = *gmtime(&t);
+        std::tm tm = *gmtime(&t);
         if (opt_utc) {
           wpt->SetCreationTime(mkgmtime(&tm));
         } else {
diff --git a/util.cc b/util.cc
index 448d02eb21161392a64de512c90d03a16d5b89c5..e67b0bcaab5b5649ed0ff6e67aabe3c20c37725c 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -568,13 +568,13 @@ le_write32(void* ptr, const unsigned value)
 */
 
 time_t
-mkgmtime(struct tm* t)
+mkgmtime(std::tm* time)
 {
   static const int      m_to_d[12] =
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
 
-  short month = t->tm_mon;
-  short year = t->tm_year + month / 12 + 1900;
+  short month = time->tm_mon;
+  short year = time->tm_year + month / 12 + 1900;
   month %= 12;
   if (month < 0) {
     year -= 1;
@@ -587,14 +587,14 @@ mkgmtime(struct tm* t)
   result += (year - 1968) / 4;
   result -= (year - 1900) / 100;
   result += (year - 1600) / 400;
-  result += t->tm_mday;
+  result += time->tm_mday;
   result -= 1;
   result *= 24;
-  result += t->tm_hour;
+  result += time->tm_hour;
   result *= 60;
-  result += t->tm_min;
+  result += time->tm_min;
   result *= 60;
-  result += t->tm_sec;
+  result += time->tm_sec;
   return (result);
 }
 
@@ -603,16 +603,16 @@ mkgmtime(struct tm* t)
  *              which is evaluated by mktime
  */
 time_t
-mklocaltime(struct tm* t)
+mklocaltime(std::tm* time)
 {
   time_t result;
-  struct tm check = *t;
+  std::tm check = *time;
 
   check.tm_isdst = 0;
   result = mktime(&check);
   check = *localtime(&result);
   if (check.tm_isdst == 1) {   /* DST is in effect */
-    check = *t;
+    check = *time;
     check.tm_isdst = 1;
     result = mktime(&check);
   }
index ccbc20fbf6d24d48b412994f79935b1ef7273e48..d7b9c133330a5fb4816e1d1eda401b3d2d501c0d 100644 (file)
@@ -521,7 +521,7 @@ std::array<int, 6> split_date(uint32_t tim)
 static time_t decode_date(uint32_t tim)
 {
   auto [sec, min, hour, mday, mon, year] = split_date(tim);
-  struct tm t;
+  std::tm t{};
 
   t.tm_sec    = sec;
   t.tm_min    = min;
diff --git a/xcsv.cc b/xcsv.cc
index 717f55c83cdc1ff2a669adf7b05afeea5cc88849..2d42b6f6b41b5282a880176a25eb223a66c0f751 100644 (file)
--- a/xcsv.cc
+++ b/xcsv.cc
@@ -29,7 +29,7 @@
 #include <cmath>                   // for fabs, pow
 #include <cstdio>                  // for snprintf, sscanf
 #include <cstdlib>                 // for strtod
-#include <cstring>                 // for strlen, strncmp, strcmp, memset
+#include <cstring>                 // for strlen, strncmp, strcmp
 #include <ctime>                   // for gmtime, localtime, time_t, mktime, strftime
 #include <optional>                // for optional
 
@@ -263,8 +263,7 @@ XcsvFormat::yyyymmdd_to_time(const QString& s)
 time_t
 XcsvFormat::sscanftime(const char* s, const char* format, bool gmt)
 {
-  struct tm stm;
-  memset(&stm, 0, sizeof(stm));
+  std::tm stm{};
 
   if (strptime(s, format, &stm)) {
     if ((stm.tm_mday == 0) && (stm.tm_mon == 0) && (stm.tm_year == 0)) {
@@ -312,7 +311,7 @@ XcsvFormat::addhms(const char* s, const char* format)
 QString
 XcsvFormat::writetime(const char* format, time_t t, bool gmt)
 {
-  static struct tm* stmp;
+  static const std::tm* stmp;
 
   if (gmt) {
     stmp = gmtime(&t);
@@ -338,8 +337,8 @@ XcsvFormat::writetime(const char* format, const gpsbabel::DateTime& t, bool gmt)
 QString
 XcsvFormat::writehms(const char* format, time_t t, bool gmt)
 {
-  static struct tm no_time = tm();
-  static struct tm* stmp = &no_time;
+  static const std::tm no_time{};
+  static const std::tm* stmp = &no_time;
 
   if (gmt) {
     stmp = gmtime(&t);